package com.valsong.sandbox.mock.invoker;

import com.valsong.sandbox.mock.exception.MockFailedException;
import groovy.lang.GroovyCodeSource;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;


/**
 * Groovy文件执行器
 *
 * @author Val Song
 */
@Slf4j
public class GroovyFileInvoker extends AbstractGroovyInvoker {

    @Override
    protected GroovyCodeSource getGroovyCodeSource(String script) {
        File sourceFile = new File(script);
        try {
            return new GroovyCodeSource(sourceFile);
        } catch (IOException e) {
            String errorMsg = String.format("Get groovyCodeSource failed! filePath:[%s]", script);
            throw new MockFailedException(errorMsg, e);
        }
    }
}
